package net.jxtz.jxtzos.spider.process;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.jxtz.jxtzos.entity.spider.SpiderLogServe;
import net.jxtz.jxtzos.spider.BaseSpiderProcessor;
import net.jxtz.jxtzos.spider.SpiderDefinition;
import net.jxtz.jxtzos.spider.SpiderFactory;
import net.jxtz.jxtzos.util.ResponseUtil;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author lmh
 * @version 1.0
 * @date 2021-11-20 11:33
 */

@Component
@Slf4j
public class LandChinaSpiderProcessor implements BaseSpiderProcessor {

    public final static String SPIDER_NAME = "landChina";

    private final static CloseableHttpClient HTTP_CLIENT = HttpClients.createDefault();

    public LandChinaSpiderProcessor() {
        SpiderFactory instance = SpiderFactory.getInstance();
        instance.putSpiderProcess(SPIDER_NAME, this);
        SpiderDefinition jingdong = new SpiderDefinition("中国土地市场", SPIDER_NAME);
        instance.putSpiderDefinition(SPIDER_NAME, jingdong);
    }

    private boolean process(Integer page) {
        String url = "https://api.landchina.com/tGdxm/result/list";
        final JSONObject jsonObject = new JSONObject();
        jsonObject.put("endDate", "2016-06-30 23:59:59");
        jsonObject.put("pageNum", page);
        jsonObject.put("pageSize", 10);
        jsonObject.put("startDate", "2013-05-01 00:00:00");
        jsonObject.put("xzqDm", 3604);
        final String s = httpClient(url, jsonObject);
        log.info(s);
        final JSONObject json = JSONObject.parseObject(s);
        final JSONObject data = json.getJSONObject("data");
        final JSONArray list1 = data.getJSONArray("list");
        log.info(list1.size() + "");
//        for (int i = 0; i < list1.size(); i++) {
//            final JSONObject jsonObject1 = list1.getJSONObject(i);
//            final String gdGuid = jsonObject1.getString("gdGuid");
//            String detailsUrl = "https://api.landchina.com/tGdxm/result/detail";
//            final JSONObject jsonObject2 = new JSONObject();
//            jsonObject2.put("gdGuid", gdGuid);
////            jsonObject2.put("landType", 3);
//            final String s1 = httpClient(detailsUrl, jsonObject2);
//            log.info(s1);
////            final JSONObject jsonObject3 = JSONObject.parseObject(s1);
////            final JSONObject data1 = jsonObject3.getJSONObject("data");
////            log.info(data1.toJSONString());
//        }
        return false;
    }

    @SneakyThrows
    private String httpClient(String url, JSONObject jsonObject) {
        TimeUnit.SECONDS.sleep(1);
        final HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Accept", "application/json, text/plain, */*");
        httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
//        httpPost.setHeader("Content-Length", "120");
        httpPost.setHeader("Content-Type", "application/json");
        httpPost.setHeader("Hash", "18782266fc032ad26ac4fd0dbc01be0d3e3ad20f4233bd336e08fa0130a39e32");
        httpPost.setHeader("Host", "api.landchina.com");
        httpPost.setHeader("Origin", "https://www.landchina.com");
        httpPost.setHeader("Referer", "https://www.landchina.com/");
        httpPost.setHeader("sec-ch-ua", "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"96\", \"Google Chrome\";v=\"96\"");
        httpPost.setHeader("sec-ch-ua-platform", "\"macOS\"");
        httpPost.setHeader("Sec-Fetch-Mode", "cors");
        httpPost.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36");

        httpPost.setEntity(new StringEntity(jsonObject.toJSONString()));

        final CloseableHttpResponse execute = HTTP_CLIENT.execute(httpPost);
        return ResponseUtil.entityToString(execute.getEntity());
    }

    @Override
    public void start(SpiderLogServe spiderLogServe, SpiderDefinition spiderDefinition) throws IOException {
        for (int i = 0; i < 10; i++) {
            final boolean process = process(i + 1);
        }
    }
}
